package com.neusoft.neu.his.myhis;

import com.neusoft.neu.his.myhis.entity.Person;
import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.Date;

public class TableApplication1 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        TableView<Person> tableView =new TableView<>();

        TableColumn<Person , Integer> idColumn =new TableColumn<>("编号");
        idColumn.setCellValueFactory(  new PropertyValueFactory<>("id"));
        tableView.getColumns().add(idColumn);


        TableColumn<Person, String> column1 =
                new TableColumn<>("First Name");

        column1.setCellValueFactory(
                new PropertyValueFactory<>("firstName"));


        TableColumn<Person, String> column2 =
                new TableColumn<>("Last Name");

        column2.setCellValueFactory(
                new PropertyValueFactory<>("lastName"));
        column2.setCellFactory(TextFieldTableCell.<Person>forTableColumn());

        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);

        TableColumn<Person , Date> hireDateColumn =new TableColumn<>("入职时间");
        hireDateColumn.setCellValueFactory(  new PropertyValueFactory<>("hiredate"));
        tableView.getColumns().add(hireDateColumn);


        tableView.setPlaceholder(
                new Label("No rows to display"));
        TableView.TableViewSelectionModel<Person> selectionModel =
                tableView.getSelectionModel();
        // set selection mode to multiple rows
        selectionModel.setSelectionMode(
                SelectionMode.MULTIPLE);

        tableView.getItems().add(
                new Person("John", "Doe"));
        tableView.getItems().add(
                new Person("Jane", "Deer"));

        VBox vbox = new VBox();

        Button button  =  new Button("Get Selected Rows");
        button.setOnAction( event -> {
            ObservableList selectedItems =
                    selectionModel.getSelectedItems();

            selectedItems.addListener(
                    new ListChangeListener<Person>() {
                        @Override
                        public void onChanged(
                                Change<? extends Person> change) {
                            System.out.println(
                                    "Selection changed: " + change.getList());
                        }
                    });
            ObservableList<Integer> selectedIndices =
                    selectionModel.getSelectedIndices();
            System.out.println( selectedIndices );
        }  );

        Button button1 =  new Button("Edit");
        button1.setOnAction( event -> {
            tableView.setEditable(true);
        });

        HBox hBox =new HBox();
        hBox.getChildren().add(button);
        hBox.getChildren().add(button1);

        vbox.getChildren().add(hBox);

        vbox.getChildren().add(tableView);

        Scene scene = new Scene(vbox , 500,600);

        primaryStage.setScene(scene) ;

        primaryStage.show();



    }
}















